<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Ads/classes/PhpFox_ComponentAdsList.class.php, $Author:  $ */



/** Component for listing ads
 * @version $Id: PhpFox_ComponentAdsList.class.php 46 2007-12-14 23:12:21Z phpfoxsv $
 * @package module.ads
 *
 */
class PhpFox_Mod_Ads_ComponentAdsList extends BaseComponent
{
    function process()
    {
    	$oAdsService = &$this->_oModule->getService('Ads');
        $oAdsItem = &$this->_oModule->getItem('Ads');
        /* @var $oAdsItem PhpFox_Mod_Ads_ItemAds */
        App::loadClass('Pager');
        $oReq = &App::getRequest();
        $oUrl = &App::getUrl();
        $oTpl = &$this->_oModule->getTemplate();
        $oSecurityService = &App::getModuleService('Account', 'Security');
        /* @var $oSecurityService PhpFox_Mod_Account_ServiceSecurity */
        if (!$oSecurityService->hasStaffPerm('MENU_edit_adds'))
        {
            App::gotoUrl('admin.main');
        }

        $iPage = $oReq->getInt('page', 1);
        if ($oReq->getAction() == 'update')
        {
            $iAdsId = $oReq->getInt('id');
            $iType = $oReq->get('type');
            if ($oAdsItem->load($iAdsId))
            {
                $oAdsItem->aData['text'] = $oReq->get('text');
                $oAdsItem->update(array('text'));
            }
            $oAdsService->deleteCached($iType);
            App::gotoUrl('admin.extra.ads', array('page' => $iPage), App::format('Mod_Ads.info.ads_updated'), false, 'ads');
        }
        if ($iAdsId = $oReq->getInt('delete'))
        {
            $oAdsItem->delete($iAdsId);
            $iType = $oReq->get('type');
            $oAdsService->deleteCached($iType);
            App::gotoUrl('admin.extra.ads', array('page' => $iPage), App::format('Mod_Ads.info.ads_deleted'), false, 'ads');
        }
        $iPageSize = App::getParam('admin.pagesize');
        $sSort = 'id DESC';
        list($aItems, $iCnt) = $oAdsItem->getList(array(), $iPage, $iPageSize, $sSort);
        $oPager = &new Pager($iCnt, $iPage, $iPageSize);
        $iFrom = $oPager->getOffset($iPage, $iPageSize);
        $iTo = (($iFrom + $iPageSize) > $iCnt) ? $iCnt : ($iFrom + $iPageSize);
        $oTpl->assignSrc('aItems', $aItems);
        $oTpl->assign(array(
            'iFrom'   => ($iCnt) ? ($iFrom + 1) : 0,
        	'iTo'     => $iTo,
        	'iCnt'    => $iCnt,
        	'aPaging' => ($iCnt) ? $oPager->getInfo($oUrl) : '',
        ));
        return $oTpl->fetch('AdsList.html');
    }
}
?>